package com.itheima.web.controller.cargo;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.itheima.domain.cargo.*;
import com.itheima.service.cargo.ContractService;
import com.itheima.service.cargo.ExportProductService;
import com.itheima.service.cargo.ExportService;
import com.itheima.service.cargo.PackingService;
import com.itheima.utils.DownloadUtil;
import com.itheima.web.controller.BaseController;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("/cargo/packing")
public class PackingController extends BaseController {

    @Reference
    private ContractService contractService;

    @Reference
    private ExportService exportService;

    @Reference
    private ExportProductService exportProductService;

    @Reference
    private PackingService packingService;  //装箱单


    @RequestMapping(value = "/toPacking", name = "跳转装箱单新增页面")
    public String toPacking(String id) {
        String[] exports = id.split(",");
        Double jingZhong = 0d;
        Double tiJi = 0d;
        Double maoZhong = 0d;
        Double feiYong = 0d;
//        String contractids=null;
        for (String exportId : exports) {
            Export export = exportService.findById(exportId);
            String contractIds = export.getContractIds();

//            if (StringUtils.isNotEmpty(contractIds)) {
//                contractids += contractIds+",";
//            }
            String[] contracts = contractIds.split(",");

            //根据合同获取总费用
            for (String contractId : contracts) {
                Contract contract = contractService.findById(contractId);
                feiYong+=contract.getTotalAmount();
            }
            //根据货物获取总净重,毛重,体积
            List<ExportProduct> exportProducts = export.getExportProducts();
            for (ExportProduct exportProduct : exportProducts) {
               jingZhong+=exportProduct.getNetWeight();
               maoZhong+=exportProduct.getGrossWeight();
               tiJi += (exportProduct.getSizeHeight()*exportProduct.getSizeLength()*exportProduct.getSizeWidth());
            }
        }

        Packing packing = new Packing();

        packing.setTiji(16+tiJi.toString());
        packing.setJingzhong(20+jingZhong.toString());
        packing.setMaozhong(25+maoZhong.toString());
        packing.setFeiyong(feiYong.toString());
//        String substring = contractids.substring(4);
        packing.setExportIds(id);


        //回显合同id集合
        request.setAttribute("id", id);

        request.setAttribute("packing",packing);

        return "/cargo/packing/packing-add";
    }


    //添加 或 修改
    @RequestMapping("/edit")
    public String edit(Packing packing){
        System.out.println(packing);
        packing.setPackingListId(UUID.randomUUID().toString());
        packing.setCompanyId(getCompanyId());
        packing.setCompanyName(getCompanyName());
        packing.setState(0l);

        packingService.save(packing);
        return "redirect:/cargo/packing/list.do";
    }
    //分页显示
    @RequestMapping("/list")
    public String list(
            @RequestParam(defaultValue = "1", name = "page") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize){
        PackingExample packingListExample = new PackingExample();
        PackingExample.Criteria criteria = packingListExample.createCriteria();
        criteria.andCompanyIdEqualTo(getCompanyId());
        PageInfo info = packingService.findByPage(pageNum, pageSize,packingListExample);
        request.setAttribute("page", info);
        return "/cargo/packing/packing-list";
    }

//    @RequestMapping(value = "/toUpdate", name = "跳转报运单编辑页面")
//    public String toUpdate(String id) {
//        //1. 根据id查询当前报运单信息
//        Export export = exportService.findById(id);
//        request.setAttribute("export", export);
//
//        //2. 根据报运单id查询当前报运单下的所有货物
//        List<ExportProduct> eps = exportProductService.findByExportId(id);
//        request.setAttribute("eps", eps);
//
//        //3. 转发到修改页面
//        return "/cargo/export/export-update";
//    }
//

//
    @RequestMapping(value = "/delete", name = "装箱单删除")
    public String delete(String id) {
        //调用service删除

        Packing packing = packingService.findById(id);
        if (packing.getState()==0){
            packingService.delete(id);
        }

        //重定向到list方法
        return "redirect:/cargo/packing/list.do";
    }

    @RequestMapping(value = "/exportPdf", name = "装箱单下载")
    public void exportPdf(String id) throws JRException, IOException {
        //0. 获取模板
        String realPath = session.getServletContext().getRealPath("/jasper/demo011.jasper");
        //1. 获取数据 条件 报运单id    结果 报运单信息和报运单下货物信息
        //1-1) 报运单信息
        Packing packing = packingService.findById(id);
        Map<String, Object> map = BeanUtil.beanToMap(packing);
        List<Packing> list = new ArrayList<>();
        list.add(packing);
        JRDataSource dataSource = new JRBeanCollectionDataSource(list);
        //2  向模板填充数据
        JasperPrint jasperPrint = JasperFillManager.fillReport(realPath, map,dataSource);
        //3.输出到浏览器
        //JasperExportManager.exportReportToPdfStream(jasperPrint, response.getOutputStream());
        //3. 文件下载
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        JasperExportManager.exportReportToPdfStream(jasperPrint, byteArrayOutputStream);
        DownloadUtil.download(byteArrayOutputStream,response,"报运单.pdf");
    }
}
